package com.zyj.check.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zyj.check.domain.AssetsCheck;
import com.zyj.check.domain.AssetsCheckDetail;
import com.zyj.check.service.IAssetsCheckService;
import com.zyj.check.service.IExcelImage;
import com.zyj.common.annotation.Log;
import com.zyj.common.core.controller.BaseController;
import com.zyj.common.core.domain.AjaxResult;
import com.zyj.common.core.page.TableDataInfo;
import com.zyj.common.enums.BusinessType;
import com.zyj.common.utils.DateUtils;
import com.zyj.check.service.impl.ExcelImage;
import com.zyj.common.utils.poi.ExcelUtil;
import com.zyj.system.service.ISysDeptService;
import com.zyj.system.service.ISysDictDataService;
import com.zyj.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 资产盘点Controller
 * 
 * @author zhuyujie
 * @date 2022-05-17
 */
@Controller
@RequestMapping("/assets/check")
public class AssetsCheckController extends BaseController
{
    private String prefix = "assets/check";

    @Autowired
    private IAssetsCheckService assetsCheckService;

    @Autowired
    private ISysUserService iSysUserServiceSysUserService;


    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IExcelImage excelImage;


    @GetMapping()
    public String check()
    {
        return prefix + "/check";
    }

    /**
     * 查询资产盘点列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetsCheck assetsCheck)
    {
        startPage();
        List<AssetsCheck> list = assetsCheckService.selectAssetsCheckList(assetsCheck);
        return getDataTable(list);
    }

    @PostMapping("/dataillist")
    @ResponseBody
    public TableDataInfo dataillist(AssetsCheckDetail assetsCheck)
    {
        startPage();
        List<AssetsCheckDetail> list = assetsCheckService.selectAssetsCheckDetailList(assetsCheck);
        return getDataTable(list);
    }

    /**
     * 导出资产盘点列表
     */
    @Log(title = "资产盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetsCheck assetsCheck)
    {
        List<AssetsCheck> list = assetsCheckService.selectAssetsCheckList(assetsCheck);
        ExcelUtil<AssetsCheck> util = new ExcelUtil<AssetsCheck>(AssetsCheck.class);
        return util.exportExcel(list, "资产盘点数据");
    }

    /**
     * 新增资产盘点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资产盘点
     */
    @Log(title = "资产盘点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetsCheck assetsCheck)
    {
        assetsCheck.setCheckUserid(getUserId());
        assetsCheck.setCreateBy(getSysUser().getUserName());
        return toAjax(assetsCheckService.insertAssetsCheck(assetsCheck));
    }

    /**
     * 修改资产盘点
     */
    @GetMapping("/edit/{checkId}")
    public String edit(@PathVariable("checkId") Long checkId, ModelMap mmap)
    {
        AssetsCheck assetsCheck = assetsCheckService.selectAssetsCheckByCheckId(checkId);
        if(assetsCheck.getCheckDepts()!=null && !"".equals(assetsCheck.getCheckDepts())) {
            assetsCheck.setCheckDeptsName(deptService.selectDeptById(Long.parseLong(assetsCheck.getCheckDepts())).getDeptName());
        }
        mmap.put("assetsCheck", assetsCheck);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产盘点
     */
    @Log(title = "资产盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetsCheck assetsCheck)
    {
        return toAjax(assetsCheckService.updateAssetsCheck(assetsCheck));
    }

    /**
     * 删除资产盘点
     */
    @Log(title = "资产盘点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(assetsCheckService.deleteAssetsCheckByCheckIds(ids));
    }


    @Log(title = "完结资产盘点", businessType = BusinessType.OTHER)
    @PostMapping( "/godown")
    @ResponseBody
    public AjaxResult godown(String ids)
    {
        AssetsCheck assetsCheck = new AssetsCheck();
        assetsCheck.setCheckId(Long.parseLong(ids));
        assetsCheck.setCheckStatus("2");
        return toAjax(assetsCheckService.updateAssetsCheck(assetsCheck));
    }

    /**
     * 资产盘点详情
     */
    @GetMapping("/detail/{checkId}")
    public String detail(@PathVariable("checkId") Long checkId, ModelMap mmap)
    {
        AssetsCheck assetsCheck = assetsCheckService.selectAssetsCheckByCheckId(checkId);
        if(assetsCheck.getCheckDepts()!=null && !"".equals(assetsCheck.getCheckDepts())) {
            assetsCheck.setCheckDeptsName(deptService.selectDeptById(Long.parseLong(assetsCheck.getCheckDepts())).getDeptName());
        }else{
            assetsCheck.setCheckDeptsName("所有");
        }
        //查询已经完成和未完成
        int nofinash = assetsCheckService.selectAssetsCheckDetailCount(checkId,0);
        int finash = assetsCheckService.selectAssetsCheckDetailCount(checkId,1);
        String type = "";
        String status = "";
        Map map = new HashMap<>();
        if(assetsCheck.getCheckAssetsType()!=null && !"".equals(assetsCheck.getCheckAssetsType())){
            String types = assetsCheck.getCheckAssetsType();
            String typesarray [] = types.split(",");
            for(int i=0;i<typesarray.length;i++){
                String t = typesarray[i];
                type += dictDataService.selectDictLabel("assets_type", t)+",";
            }
            type = type.substring(0,type.length()-1);
        }else{
            type = "所有";
        }
        if(assetsCheck.getCheckAssetsStatus()!=null && !"".equals(assetsCheck.getCheckAssetsStatus())){
            String statuss = assetsCheck.getCheckAssetsStatus();
            String statussarray [] = statuss.split(",");
            for(int i=0;i<statussarray.length;i++){
                String t = statussarray[i];
                status += dictDataService.selectDictLabel("assets_status", t)+",";
            }
            status = status.substring(0,status.length()-1);
        }else{
            status = "所有";
        }
        mmap.put("assetsCheck", assetsCheck);
        mmap.put("nofinash", nofinash);
        mmap.put("finash", finash);
        mmap.put("type", type);
        mmap.put("status", status);

        //资产分类柱状图数据
        List<String> assetsTypeDataList = new ArrayList<>();//分类
        List<Integer> assetsTypeValueList1 = new ArrayList<>();//已盘
        List<Integer> assetsTypeValueList2 = new ArrayList<>();//未盘
        List<AssetsCheck> checkbytypelist = assetsCheckService.selectAssetsCheckDetailsByType(checkId);
        if(checkbytypelist!=null && !checkbytypelist.isEmpty()){
            for(int i=0;i<checkbytypelist.size();i++){
                AssetsCheck checktype = checkbytypelist.get(i);
                String typename = checktype.getCheckAssetsType();
//                String ischeck = checktype.getCheckStatus();//1已盘 0未盘
                int number = checktype.getCheckNum();
                System.out.println(typename+"|"+number);
                assetsTypeDataList.add(typename);
                int checkbytypeNumber1 = assetsCheckService.selectAssetsCheckDetailsByTypeCount(checkId, typename,"1");
                assetsTypeValueList1.add(checkbytypeNumber1);
                int checkbytypeNumber2 = assetsCheckService.selectAssetsCheckDetailsByTypeCount(checkId, typename,"0");
                assetsTypeValueList2.add(checkbytypeNumber2);
            }
        }
        mmap.put("assetsTypeDataList", assetsTypeDataList.toArray());
        mmap.put("assetsTypeValueList1", assetsTypeValueList1.toArray());
        mmap.put("assetsTypeValueList2", assetsTypeValueList2.toArray());
        return prefix + "/detail";
    }



    /**
     * 手工盘点
     */
    @GetMapping("/checkit/{id}")
    public String checkit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("checkDetailId", id);
        return prefix + "/checkit";
    }


    /**
     * 保存资产盘点
     */
    @Log(title = "手工保存资产盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/checkSave")
    @ResponseBody
    public AjaxResult checkSave(AssetsCheckDetail assetsCheckDetail)
    {
        assetsCheckDetail.setUserId(getUserId());
        assetsCheckDetail.setUserName(iSysUserServiceSysUserService.selectUserById(getUserId()).getUserName());
        assetsCheckDetail.setCreateBy(getLoginName());
        return toAjax(assetsCheckService.checkSave(assetsCheckDetail));
    }


    @Log(title = "下载盘点报告", businessType = BusinessType.OTHER)
    @PostMapping( "/exportReport")
    @ResponseBody
    public AjaxResult exportReport(String ids,String assetsTypeimg,String checkinfoimg)
    {
        AssetsCheck assetsCheck = assetsCheckService.selectAssetsCheckByCheckId(Long.parseLong(ids));
        Map info = new HashMap<>();
        info.put("title",assetsCheck.getCheckName());
        info.put("exporttime", DateUtils.getDate());
        info.put("checkNo", assetsCheck.getCheckNo());
        info.put("checkTime", assetsCheck.getCheckTime());
        String type = "";
        String status = "";
        if(assetsCheck.getCheckAssetsType()!=null && !"".equals(assetsCheck.getCheckAssetsType())){
            String types = assetsCheck.getCheckAssetsType();
            String typesarray [] = types.split(",");
            for(int i=0;i<typesarray.length;i++){
                String t = typesarray[i];
                type += dictDataService.selectDictLabel("assets_type", t)+",";
            }
            type = type.substring(0,type.length()-1);
        }else{
            type = "所有";
        }
        info.put("assetsType", type);
        if(assetsCheck.getCheckAssetsStatus()!=null && !"".equals(assetsCheck.getCheckAssetsStatus())){
            String statuss = assetsCheck.getCheckAssetsStatus();
            String statussarray [] = statuss.split(",");
            for(int i=0;i<statussarray.length;i++){
                String t = statussarray[i];
                status += dictDataService.selectDictLabel("assets_status", t)+",";
            }
            status = status.substring(0,status.length()-1);
        }else{
            status = "所有";
        }
        info.put("assetsStatus", status);
        if(assetsCheck.getCheckDepts()!=null && !"".equals(assetsCheck.getCheckDepts())) {
            info.put("detpName",deptService.selectDeptById(Long.parseLong(assetsCheck.getCheckDepts())).getDeptName());
        }else{
            info.put("detpName", "所有");
        }

        AssetsCheckDetail assetsCheckDetail = new AssetsCheckDetail();
        assetsCheckDetail.setCheckId(Long.parseLong(ids));
        List<AssetsCheckDetail> list = assetsCheckService.selectAssetsCheckDetailList(assetsCheckDetail);

        return excelImage.creatExportCheckReport(info, list, "盘点报告", assetsCheck.getCheckName()+"的盘点报告", assetsTypeimg,checkinfoimg);
    }

}
